import { uploadImages } from '@/api/guide.js'
// import {offlineSaveImg} from '@/api/tools'
const fileEditor = {
  data() {
    return {
      img_timer: null,
      s_img_dropdown_open: false,
    }
  },
  methods: {
    $imgAdds(pos, $file, origin) {
      if (!this.img_file) {
        this.img_file = []
      }
      this.img_file[pos] = $file;
      //从系统中加入已上传的文件
      if (this.imgFromSystem) {
        let _res = '/Learn' + this.useImagePath;
        // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
        if (this && this.$refs && this.$refs.md) {
          this.$refs.md.$img2Url(pos, _res);
        }
      } else {
        // 第一步.将图片上传到服务器.
        let formdata = new FormData();
        formdata.append('image', $file);
        formdata.append("file", $file)
        uploadImages(formdata).then((res) => {
          if (res.code == 1) {
            //let _res = 'http://192.168.1.100:9999'+res.image;
            let _res = '/Learn' + res.data;
            // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
            if (this && this.$refs && this.$refs.md) {
              this.$refs.md.$img2Url(pos, _res);
            }
          } else {
            console.log('文件上传失败')
          }
        })
      }
    },
    $imgDels(pos) {
      delete this.img_file[pos];
    },
    $click(a) {
      console.log(a)
    },
    $mouseleave_img_dropdown() {
      let vm = this
      this.img_timer = setTimeout(function () {
        vm.s_img_dropdown_open = false
      }, 200)
    },
    $mouseenter_img_dropdown() {
      if (this.img_timer) {
        clearTimeout(this.img_timer)
      }
      this.s_img_dropdown_open = true
    },
    $imgAdd($e) {
      this.imgFromSystem = false;
      this.$refs.md.$refs.toolbar_left.$imgAdd($e);
    },
  }
}
export default fileEditor
